package com.sdkx.app.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.sdkx.app.R;
import com.sdkx.app.adapter.MainMenuAdapter;

public class MainMenuFragmentView extends BaseFragment {
    private View rootView;
    private ListView lvGroup;
    //主目录
    private String[] names = {"航线管理", "航点管理", "标记管理", "报警管理", "航迹管理", "空域管理", "地图管理"};
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (null != rootView) {
            ViewGroup parent = (ViewGroup) rootView.getParent();
            if (null != parent) {
                parent.removeView(rootView);
            }
        } else {
            initView(inflater, container);
        }
        return rootView;
    }

    private void initView(LayoutInflater inflater, ViewGroup container) {
        rootView = inflater.inflate( R.layout.fragment_menu, container,
                false);
        lvGroup = (ListView) rootView.findViewById(R.id.lvGroup);
        MainMenuAdapter menuadapter = new MainMenuAdapter(getActivity(), names);
        lvGroup.setAdapter(menuadapter);
        lvGroup.setOnItemClickListener(new MyMainMenuAdapterViewListener());
    }
    //主菜单点击事件
    private class MyMainMenuAdapterViewListener implements AdapterView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//            ((MainActivity)getActivity()).ShowMainMenu(position+1);
            Class<? extends Fragment> obj = null;
            switch (position){
                case 0:
                    obj = RoutesFragmentView1.class;
                    break;
                case 1:
                    obj = WaypointFragment.class;
                    break;
                case 2:
                    obj = MarkFragment.class;
                    break;
                case 3:
                    obj = AlarmFragment.class;
                    break;
                case 4:
                    obj = TrackFragment.class;
                    break;
                case 5:
                    obj = AirspaceGeneralFragment.class;
                    break;
                case 6:
                    obj =  MapFragment.class;
            }
            if(obj != null) {
                startFragment(obj);
            }
        }
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        rootView = null;
    }
}
